package com.gg.user.gguser.service.impl;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.TypeReference;
import com.gg.header.getHeaderUser;
import com.gg.user.gguser.dao.GgBusinessOpportunityDaoMapper;
import com.gg.user.gguser.dao.GgInformationDaoMapper;
import com.gg.user.gguser.dataobject.GgBusinessOpportunityDtaobject;
import com.gg.user.gguser.dataobject.GgInformationDataobject;
import com.gg.user.gguser.dataobject.GgItemsOnlyDataobject;
import com.gg.user.gguser.service.GgBusinessOpportunityService;
import com.gg.user.gguser.service.GgDeletcInformationService;
import com.gg.user.gguser.service.GgItemsOnlyService;
import com.gg.vo.return_format;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class GgBusinessOpportunityServiceImpl implements GgBusinessOpportunityService {

    @Autowired
    private GgBusinessOpportunityDaoMapper ggBusinessOpportunityDaoMapper;

    @Autowired
    private GgInformationDaoMapper ggInformationDaoMapper;

    @Autowired
    private GgItemsOnlyService ggItemsOnlyService;

    @Autowired
    private GgDeletcInformationService ggDeletcInformationService;

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Object addBusiness(HttpServletResponse response, HttpServletRequest request, Integer id) throws Exception {
        if(id != null && id > 0 ) {
            // 查出信息详情
            GgInformationDataobject ggInformationDataobject = ggInformationDaoMapper.getGgInformationDataobjectItems(id);
            if(ggInformationDataobject==null){
                return return_format.return_format_error(100003,"没有这个信息哦！");
            }else {
                try {
                    // 获取用户信息
                    // 从cookice信息中过去用户信息 在这里只用要id就好
                    String cookiceUser = getHeaderUser.getHader(response, request, "gg");
                    String ggId = JSONObject.parseObject(cookiceUser).getString("ggId");
                    // 插入商机
                    ggBusinessOpportunityDaoMapper.yaddBusinessOpportunity(ggInformationDataobject.getGgItemsToken(), ggInformationDataobject.getGgIName(), ggInformationDataobject.getGgIProvince(), ggInformationDataobject.getGgICity(), ggInformationDataobject.getGgIDetailed(), ggInformationDataobject.getGgIHandle(), ggInformationDataobject.getGgIContact(), ggInformationDataobject.getGgIState(), ggInformationDataobject.getGgILevel(), ggInformationDataobject.getGgIGender(), ggInformationDataobject.getGgIPosition(), ggInformationDataobject.getGgITelephone(), ggInformationDataobject.getGgIFax(), ggInformationDataobject.getGgIMailbox(), ggInformationDataobject.getGgIQq(), ggInformationDataobject.getGgIWechat(), ggInformationDataobject.getGgIRemarks(), ggInformationDataobject.getGgId(), ggId, ggInformationDataobject.getGgITime(),ggInformationDataobject.getGgIAreaid(),ggInformationDataobject.getGgIZipcode(),ggInformationDataobject.getGgIWebsite(),ggInformationDataobject.getGgISource());

                    // 从信息里面删除
                    ggInformationDaoMapper.delectGgInformationDataobjectItems(id);
                    return return_format.return_format_All(0,"","");
                }catch (Exception e){
                    return return_format.return_format_error(100002,"服务有问题了!");
                }
            }

        }else {
            return return_format.return_format_error(100002,"用户自有数据是允许爬虫，但是参数不要瞎传！");
        }
    }

    // 移除 商机移入信息
    @Override
    @Transactional(rollbackFor = Exception.class)
    public Object removebusiness(HttpServletResponse response, HttpServletRequest request, Integer id) throws Exception{
        if(id != null && id > 0 ) {
            GgBusinessOpportunityDtaobject ggBusinessOpportunityDtaobject = ggBusinessOpportunityDaoMapper.getBusinessOpportunity(id);// 获取商机信息
            if(ggBusinessOpportunityDtaobject==null){
                return return_format.return_format_error(100003,"没有这个信息哦！");
            }else {
                ggInformationDaoMapper.addInformation(ggBusinessOpportunityDtaobject.getGgItemsToken(),ggBusinessOpportunityDtaobject.getGgBoName(),ggBusinessOpportunityDtaobject.getGgBoProvince(),ggBusinessOpportunityDtaobject.getGgBoCity(),ggBusinessOpportunityDtaobject.getGgBoDetailed(),ggBusinessOpportunityDtaobject.getGgBoHandle(),ggBusinessOpportunityDtaobject.getGgBoContact(),ggBusinessOpportunityDtaobject.getGgBoState(),ggBusinessOpportunityDtaobject.getGgBoLevel(),ggBusinessOpportunityDtaobject.getGgBoGender(),ggBusinessOpportunityDtaobject.getGgBoPosition(),ggBusinessOpportunityDtaobject.getGgBoTelephone(),ggBusinessOpportunityDtaobject.getGgBoFax(),ggBusinessOpportunityDtaobject.getGgBoMailbox(),ggBusinessOpportunityDtaobject.getGgBoQq(),ggBusinessOpportunityDtaobject.getGgBoWechat(),ggBusinessOpportunityDtaobject.getGgBoRemarks(),ggBusinessOpportunityDtaobject.getGgId(),ggBusinessOpportunityDtaobject.getGgBoTime(), ggBusinessOpportunityDtaobject.getGgBoAreaid(),ggBusinessOpportunityDtaobject.getGgBoZipcode(),ggBusinessOpportunityDtaobject.getGgBoWebsite(),ggBusinessOpportunityDtaobject.getGgBoSource());
                // 从商机里面删除
                ggBusinessOpportunityDaoMapper.delectGgBusinessOpportunityItems(id);
                return return_format.return_format_All(0,"","");
            }

        }else {
            return return_format.return_format_error(100002,"用户自有数据是允许爬虫，但是参数不要瞎传！");
        }
    }

    @Override
    public Object updateBusiness() {
        return null;
    }

    @Override
    public Object getBusiness(HttpServletResponse response, HttpServletRequest request,Integer page,Integer pageNumber,String name, Integer type,Integer state,String remarks) throws Exception{
        if(page != null && page > 0 && pageNumber != null && pageNumber > 0 ){
            // 获取用户信息
            // 从cookice信息中过去用户信息 在这里只用要id就好
            String cookiceUser = getHeaderUser.getHader(response,request,"gg");
            String ggIdUser= JSONObject.parseObject(cookiceUser).getString("ggId");
            Integer ggGrade= JSONObject.parseObject(cookiceUser).getInteger("ggGrade");
            // 数据列表
            name = name!=null && name.equals("") ? null:name;
            type = type!=null && type.equals("") ? null:type;
            state = state!=null && state.equals("") ? null:state;
            remarks = remarks!=null && remarks.equals("") ? null:remarks;
            if(ggGrade == 3){
                // 公司身份
               Integer god = ggBusinessOpportunityDaoMapper.getBusinessOpportunitySelectCompanyCount(ggIdUser,name,type,state,remarks);
                List<GgBusinessOpportunityDtaobject> godList = ggBusinessOpportunityDaoMapper.getInformationSelectListCompany((page-1)*pageNumber, pageNumber,ggIdUser,name,type,state,remarks);
                return return_format.return_format_All(0,return_format.return_format_data_map(page, (int) Math.ceil(god/pageNumber),pageNumber,god,godList),"");
            }
            Integer BusinessOpportunitySelectCountNumber = ggBusinessOpportunityDaoMapper.getBusinessOpportunitySelectCount(ggIdUser,name,type,state,remarks);
            List<GgBusinessOpportunityDtaobject> getBusinessOpportunitySelectList = ggBusinessOpportunityDaoMapper.getInformationSelectList((page-1)*pageNumber, pageNumber,ggIdUser,name,type,state,remarks);
            return return_format.return_format_All(0,return_format.return_format_data_map(page, (int) Math.ceil(BusinessOpportunitySelectCountNumber/pageNumber),pageNumber,BusinessOpportunitySelectCountNumber,getBusinessOpportunitySelectList),"");

        }else {
            return return_format.return_format_error(100002,"用户自有数据是允许爬虫，但是参数不要瞎传！");
        }
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Object delectBusiness(HttpServletResponse response, HttpServletRequest request,Integer id) throws Exception{
        if(id != null || id<=0 ){
            String cookiceUser = getHeaderUser.getHader(response,request,"gg");
            String ggIdUser= JSONObject.parseObject(cookiceUser).getString("ggId"); // 获取到用户id
            GgBusinessOpportunityDtaobject ggBusinessOpportunityDtaobject = ggBusinessOpportunityDaoMapper.getBusinessOpportunity(id);// 获取商机信息
            if(ggBusinessOpportunityDtaobject==null){
                return return_format.return_format_error(100002,"删除商机不存在！");
            }else {
                // 移入删除信息
                ggDeletcInformationService.addDeletcInformationService(ggBusinessOpportunityDtaobject.getGgItemsToken(), ggBusinessOpportunityDtaobject.getGgBoName(), ggBusinessOpportunityDtaobject.getGgBoProvince(), ggBusinessOpportunityDtaobject.getGgBoCity(), ggBusinessOpportunityDtaobject.getGgBoDetailed(), ggBusinessOpportunityDtaobject.getGgBoHandle(), ggBusinessOpportunityDtaobject.getGgBoContact(), ggBusinessOpportunityDtaobject.getGgBoState(), ggBusinessOpportunityDtaobject.getGgBoLevel(), ggBusinessOpportunityDtaobject.getGgBoGender(), ggBusinessOpportunityDtaobject.getGgBoPosition(), ggBusinessOpportunityDtaobject.getGgBoTelephone(), ggBusinessOpportunityDtaobject.getGgBoFax(), ggBusinessOpportunityDtaobject.getGgBoMailbox(), ggBusinessOpportunityDtaobject.getGgBoQq(), ggBusinessOpportunityDtaobject.getGgBoWechat(), ggBusinessOpportunityDtaobject.getGgBoRemarks(), ggBusinessOpportunityDtaobject.getGgId(), ggIdUser, ggBusinessOpportunityDtaobject.getGgBoTime(), ggIdUser, ggBusinessOpportunityDtaobject.getGgBoAreaid(),ggBusinessOpportunityDtaobject.getGgBoZipcode(),ggBusinessOpportunityDtaobject.getGgBoWebsite(),ggBusinessOpportunityDtaobject.getGgBoSource());
                // 从商机里面删除
                ggBusinessOpportunityDaoMapper.delectGgBusinessOpportunityItems(id);
                return return_format.return_format_All(0,"","");
            }
        }else {
            return return_format.return_format_error(100002,"用户自有数据是允许爬虫，但是参数不要瞎传！");
        }
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Object addBusinessAll(HttpServletResponse response, HttpServletRequest request,String name,String ggBoNextTime, String sex, String position, String phone, String mailbox,String qq, String weChat,String provid, String cityid,String areaid,String address,Integer level,Integer state,String remarks,String username,String fax,String contact,String zipcode,String website,String source) throws Exception {
        if(name != null && name != "" && username != null && username != "" && address != null && address != "" && phone!=null  && phone != ""){
            try {
                String cookiceUser = getHeaderUser.getHader(response,request,"gg");
                String ggIdUser= JSONObject.parseObject(cookiceUser).getString("ggId");
                // 弄一个商机、信息公共唯一标识符
                GgItemsOnlyDataobject ggItemsOnlyDataobjectToken = ggItemsOnlyService.judgeItemsOnly(response,request,"商机");
                if (ggItemsOnlyDataobjectToken != null){
                    ggBusinessOpportunityDaoMapper.addBusinessOpportunity(name,provid,cityid,areaid,address,contact,username,state,level,sex,position,phone,fax,mailbox,qq,weChat,remarks,ggIdUser,ggIdUser,ggItemsOnlyDataobjectToken.getGgIoOnly(), zipcode, website, source);
                    return return_format.return_format_All(0,"","");
                }else {
                    return return_format.return_format_error(100003,"插入失败请稍后重试！");
                }

            }catch (Exception e){
                return return_format.return_format_error(100003,"插入失败请稍后重试！");
            }

        }else {
            return return_format.return_format_error(100002,"用户自有数据是允许爬虫，但是参数不要瞎传！");
        }
    }

    // 查看商机详情
    @Override
    public Object getBusinessSelectItems(HttpServletResponse response, HttpServletRequest request,String token) throws IOException {
        if(token != null){
            try {
                String cookiceUser = getHeaderUser.getHader(response,request,"gg");
                String ggIdUser= JSONObject.parseObject(cookiceUser).getString("ggId");
                return return_format.return_format_All(0,ggBusinessOpportunityDaoMapper.getBusinessSelectItems(ggIdUser,token),"");
            }catch (Exception e){
                return return_format.return_format_error(100003,"服务有问题了");
            }

        }else {
            return return_format.return_format_error(100002,"用户自有数据是允许爬虫，但是参数不要瞎传！");
        }
    }

    // 修改商机
    @Override
    public Object updateBusiness(HttpServletResponse response, HttpServletRequest request, String ggItemsToken, String ggBoName, String ggBoProvince, String ggBoCity, String ggBoAreaid, String ggBoDetailed, String ggBoHandle, String ggBoContact, Integer ggBoState, Integer ggBoLevel, String ggBoGender, String ggBoPosition, String ggBoTelephone, String ggBoFax, String ggBoMailbox, String ggBoQq, String ggBoWechat,String ggBoZipcode,String ggBoWebsite,String ggBoSource) throws Exception {
        if(ggBoName != null || ggBoName != "" || ggBoContact != null || ggBoContact != "" || ggBoTelephone != null || ggBoTelephone != "" || ggBoDetailed!=null  || ggBoDetailed != ""){
            try {
                String cookiceUser = getHeaderUser.getHader(response,request,"gg");
                String ggIdUser= JSONObject.parseObject(cookiceUser).getString("ggId"); // 获取到用户id
                Integer updateInteger = ggBusinessOpportunityDaoMapper.updateBusiness(ggItemsToken, ggBoName, ggBoProvince, ggBoCity, ggBoAreaid, ggBoDetailed, ggBoHandle ,ggBoContact, ggBoState ,ggBoLevel ,ggBoGender ,ggBoPosition ,ggBoTelephone ,ggBoFax ,ggBoMailbox ,ggBoQq, ggBoWechat, ggIdUser, ggBoZipcode, ggBoWebsite, ggBoSource );
                return return_format.return_format_All(0,"","");
            }catch (Exception e){
                return return_format.return_format_error(100002,"服务有问题了！");
            }
        }else {
            return return_format.return_format_error(100002,"用户自有数据是允许爬虫，但是参数不要瞎传！");
        }

    }

    /**
     * 商机批量  移除 商机移入信息
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public Object removebusinessAll(HttpServletResponse response, HttpServletRequest request, String listid) {
        try {
            List id_list = JSON.parseObject(listid,new TypeReference<List>(){});
            if(id_list.size()!=0){
                try {
                    Integer yes = 0;  // 移动成功
                    Integer not = 0;  // 没有这个信息
                    Integer error = 0; // 错误
                    for (int i = 0;i < id_list.size(); i++){
                        try {
                            Integer id = Integer.parseInt(String.valueOf(id_list.get(i))) ;// id
                            GgBusinessOpportunityDtaobject ggBusinessOpportunityDtaobject = ggBusinessOpportunityDaoMapper.getBusinessOpportunity(id);// 获取商机信息
                            if(ggBusinessOpportunityDtaobject==null){
                                not = not + 1;
                            }else {
                                ggInformationDaoMapper.addInformation(ggBusinessOpportunityDtaobject.getGgItemsToken(),ggBusinessOpportunityDtaobject.getGgBoName(),ggBusinessOpportunityDtaobject.getGgBoProvince(),ggBusinessOpportunityDtaobject.getGgBoCity(),ggBusinessOpportunityDtaobject.getGgBoDetailed(),ggBusinessOpportunityDtaobject.getGgBoHandle(),ggBusinessOpportunityDtaobject.getGgBoContact(),ggBusinessOpportunityDtaobject.getGgBoState(),ggBusinessOpportunityDtaobject.getGgBoLevel(),ggBusinessOpportunityDtaobject.getGgBoGender(),ggBusinessOpportunityDtaobject.getGgBoPosition(),ggBusinessOpportunityDtaobject.getGgBoTelephone(),ggBusinessOpportunityDtaobject.getGgBoFax(),ggBusinessOpportunityDtaobject.getGgBoMailbox(),ggBusinessOpportunityDtaobject.getGgBoQq(),ggBusinessOpportunityDtaobject.getGgBoWechat(),ggBusinessOpportunityDtaobject.getGgBoRemarks(),ggBusinessOpportunityDtaobject.getGgId(),ggBusinessOpportunityDtaobject.getGgBoTime(),ggBusinessOpportunityDtaobject.getGgBoAreaid(),ggBusinessOpportunityDtaobject.getGgBoZipcode(),ggBusinessOpportunityDtaobject.getGgBoWebsite(),ggBusinessOpportunityDtaobject.getGgBoSource());
                                // 从商机里面删除
                                ggBusinessOpportunityDaoMapper.delectGgBusinessOpportunityItems(id);
                                yes = yes + 1;
                            }
                        }catch (Exception e){
                            error = error + 1;
                        }
                    }
                    Map dataMap = new HashMap();
                    dataMap.put("yes", yes);
                    dataMap.put("not", not);
                    dataMap.put("error", error);
                    return return_format.return_format_All(0, dataMap,"");
                }catch (Exception e){
                    return return_format.return_format_error(100002,"服务有问题了！");
                }
            }else {
                return return_format.return_format_error(100002,"用户自有数据是允许爬虫，但是参数不要瞎传！");
            }
        }catch (Exception e){
            return return_format.return_format_error(100002,"服务有问题了！");
        }


    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Object delectbusinessAll(HttpServletResponse response, HttpServletRequest request, String listid) throws Exception{
        try {
            List id_list = JSON.parseObject(listid,new TypeReference<List>(){});
            String cookiceUser = getHeaderUser.getHader(response,request,"gg");
            String ggIdUser= JSONObject.parseObject(cookiceUser).getString("ggId"); // 获取到用户id
            if(id_list.size()!=0){
                try {
                    Integer yes = 0;  // 移动成功
                    Integer not = 0;  // 没有这个信息
                    Integer error = 0; // 错误
                    for (int i = 0;i < id_list.size(); i++){
                        try {
                            Integer id = Integer.parseInt(String.valueOf(id_list.get(i))) ;// id
                            GgBusinessOpportunityDtaobject ggBusinessOpportunityDtaobject = ggBusinessOpportunityDaoMapper.getBusinessOpportunity(id);// 获取商机信息
                            if(ggBusinessOpportunityDtaobject==null){
                                not = not + 1;
                            }else {
                                // 移入删除信息
                                ggDeletcInformationService.addDeletcInformationService(ggBusinessOpportunityDtaobject.getGgItemsToken(), ggBusinessOpportunityDtaobject.getGgBoName(), ggBusinessOpportunityDtaobject.getGgBoProvince(), ggBusinessOpportunityDtaobject.getGgBoCity(), ggBusinessOpportunityDtaobject.getGgBoDetailed(), ggBusinessOpportunityDtaobject.getGgBoHandle(), ggBusinessOpportunityDtaobject.getGgBoContact(), ggBusinessOpportunityDtaobject.getGgBoState(), ggBusinessOpportunityDtaobject.getGgBoLevel(), ggBusinessOpportunityDtaobject.getGgBoGender(), ggBusinessOpportunityDtaobject.getGgBoPosition(), ggBusinessOpportunityDtaobject.getGgBoTelephone(), ggBusinessOpportunityDtaobject.getGgBoFax(), ggBusinessOpportunityDtaobject.getGgBoMailbox(), ggBusinessOpportunityDtaobject.getGgBoQq(), ggBusinessOpportunityDtaobject.getGgBoWechat(), ggBusinessOpportunityDtaobject.getGgBoRemarks(), ggBusinessOpportunityDtaobject.getGgId(), ggIdUser, ggBusinessOpportunityDtaobject.getGgBoTime(), ggIdUser, ggBusinessOpportunityDtaobject.getGgBoAreaid(),ggBusinessOpportunityDtaobject.getGgBoZipcode(),ggBusinessOpportunityDtaobject.getGgBoWebsite(),ggBusinessOpportunityDtaobject.getGgBoSource());
                                // 从商机里面删除
                                ggBusinessOpportunityDaoMapper.delectGgBusinessOpportunityItems(id);
                                yes = yes + 1;
                            }
                        }catch (Exception e){
                            error = error + 1;
                        }
                    }
                    Map dataMap = new HashMap();
                    dataMap.put("yes", yes);
                    dataMap.put("not", not);
                    dataMap.put("error", error);
                    return return_format.return_format_All(0, dataMap,"");
                }catch (Exception e){
                    return return_format.return_format_error(100002,"服务有问题了！");
                }
            }else {
                return return_format.return_format_error(100002,"用户自有数据是允许爬虫，但是参数不要瞎传！");
            }
        }catch (Exception e){
            return return_format.return_format_error(100002,"服务有问题了！");
        }
    }
}
